package com.icss.id.controller;

import com.icss.id.dao.UserDao;
import com.icss.id.pojo.Item;
import com.icss.id.pojo.Record;
import com.icss.id.pojo.User;
import com.icss.id.service.Impl.IRecordServiceImpl;
import com.icss.id.service.Impl.ItemServiceImpl;
import com.icss.id.service.Impl.UserServiceImpl;
import com.sun.org.apache.xml.internal.dtm.Axis;
import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@Component
public class RecordController {
    static Logger logger = LoggerFactory.getLogger(RecordController.class);

    @Autowired
    private IRecordServiceImpl iRecordService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ItemServiceImpl itemService;
    @Autowired
    private UserDao userDao;

    @RequestMapping("showRecord")
    public String showRecordPage(HttpServletResponse response, String carName, Timestamp start, Timestamp end, ModelMap mp, Integer pageNum) {

        Page<Record> page = iRecordService.findRecordPage(pageNum);

        logger.info("pageNum==" + pageNum);
        mp.addAttribute("pageInfo", page);

        if (carName != null && start != null || end != null) {
            Page<Record> recordByCarNum = iRecordService.findRecordByCarNum(carName, start, end, pageNum);
            mp.addAttribute("pageInfo", recordByCarNum);
        }
        response.addHeader("x-frame-options", "SAMEORIGIN");
        return "record";
    }

    @RequestMapping("updateUser")
    public String updateUserS(Integer fine, Integer score, Integer recordId) {

        userService.updateUser(fine, score, recordId);

        return "redirect:showRecord";

    }

    @RequestMapping("add")
    public String showUandIs(ModelMap mp) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        mp.addAttribute("itemDate", df.format(new Date()));
        mp.addAttribute("staus", "未处理");

        System.out.println(df);

        return "addRecord";

    }


    @RequestMapping("addUsername")
    @ResponseBody
    public List fun1() {
        List<User> user = userDao.findAll();
        return user;

    }

    @RequestMapping("addItemdelict")
    @ResponseBody
    public List fun2() {
        List<Item> item = itemService.findItem();
        return item;
    }


    @RequestMapping("addRecords")
    public String  addRecords(String username,String delict,String address,String itemDate){
        System.out.println(username);
        System.out.println(delict);
        Item byDelict = itemService.findByDelict(delict);
        Integer itemId1 = byDelict.getItemId();

        iRecordService.updateRecord(username,itemId1,address,itemDate);

        return "redirect:showRecord";
    }



}
